package com.mdinfo.gw.door.service.impl

import com.alibaba.fastjson.JSON
import com.mdinfo.base.common.bean.CommonResult
import com.mdinfo.base.common.bean.DataGrid
import com.mdinfo.base.common.bean.Ref
import com.mdinfo.base.utils.StringUtil
import com.mdinfo.base.utils.SystemUtil
import com.mdinfo.base.utils.TimeUtil
import com.mdinfo.gw.door.bean.Door
import com.mdinfo.gw.door.bean.DoorCard
import com.mdinfo.gw.door.bean.DoorCardBind
import com.mdinfo.gw.door.mapper.*
import com.mdinfo.gw.door.param.*
import com.mdinfo.gw.door.service.IDoorCardService
import com.mdinfo.gw.door.vo.*
import com.mdinfo.system.bean.SysDept
import com.mdinfo.system.bean.SysEmp
import com.mdinfo.system.mapper.SysDeptMapper
import com.mdinfo.system.mapper.SysEmpMapper
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by Administrator on 2018/1/16.
 */
@Service
@Transactional
class DoorCardServiceImpl(
        val doorCardMapper : DoorCardMapper,
        val doorCardBindMapper: DoorCardBindMapper,
        val doorMapper: DoorMapper,
        val deptMapper: SysDeptMapper,
        val empMapper: SysEmpMapper,
        val cardTimeBindMapper: DoorCardTimeBindMapper,
        val openTimeMapper: DoorOpenTimeMapper
) : IDoorCardService{

    override fun add(param: DoorCardAddParam): CommonResult<Void> {
        var doorCard = doorCardMapper.getByDevId(param.devId!!)
        if(doorCard!=null){
            return Ref.error("该卡号已经存在")
        }
        doorCard = DoorCard()
        doorCard.createTime = Date()
        doorCard.deptId = param.deptId!!
        doorCard.devId = param.devId!!
        doorCardMapper.insert(doorCard)
        return Ref.ok()
    }

    /**列表查询*/
    override fun listQuery(param: DoorCardListQueryParam)
            : CommonResult<DataGrid<DoorCardListQueryVo>>{

        val info = SystemUtil.getInfo()
        var list = doorCardMapper.listQuery(param.start!!,
                param.num!!,
                param.key,
                param.deptId,
                info.deptIdList)
        var count = doorCardMapper.countListQuery(param.key,
                param.deptId,
                info.deptIdList)

        var vos = list.map {
            var vo = DoorCardListQueryVo()
            vo.cardId = it.id
            vo.createTime = TimeUtil.formater(it.createTime)
            vo.deptId = it.deptId
            var dept:SysDept? = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""
            vo.devId = it.devId
            vo.empId = it.empId
            var emp:SysEmp? = empMapper.selectById(it.empId)
            vo.empName = emp?.name?:""

            var binds = cardTimeBindMapper.listByCardId(it.id)
            var bindDoors = binds.map {
                var bindDoor = DoorCardListQueryBindDoorVo()
                var time = openTimeMapper.selectById(it.timeId)
                var door = doorMapper.selectById(time.doorId)
                bindDoor.doorId = door.id
                bindDoor.doorName = door.name
                bindDoor.doorDevId = door.devId
                bindDoor.openTimeId = time.id
                bindDoor.openTimeName = time.name!!
                bindDoor.bindTime = TimeUtil.formater3(it.createTime)
                bindDoor.expireTime = TimeUtil.formater1(time.endDate)
                bindDoor.timePickerStart = time.startTime!!
                bindDoor.timePickerEnd = time.endTime!!
                bindDoor.weekday = time.weekday!!.replace("1","一")
                        .replace("2","二")
                        .replace("3","三")
                        .replace("4","四")
                        .replace("5","五")
                        .replace("6","六")
                        .replace("7","日")
                bindDoor.opModeStr = when(time.opMode) {
                    1 -> "单卡识别"
                    2 -> "卡加密码"
                    3 -> "定时开关门"
                    4 -> "双卡识别"
                    5 -> "定时报警"
                    6 -> "密码"
                    7 -> "首卡开门"
                    else -> ""
                }
                bindDoor
            }
            //门卡绑定的门和时间数据
            vo.doors = bindDoors
            vo
        }
        val dataGrid = DataGrid<DoorCardListQueryVo>()
        dataGrid.total = count
        dataGrid.rows = vos
        return Ref.ok(dataGrid)
    }

    /**修改门卡*/
    override fun modify(param: DoorCardModifyParam): CommonResult<Void>{

        var doorCard:DoorCard? = doorCardMapper.selectById(param.cardId)

        doorCard?:return Ref.error("错误的门卡编号")

        doorCard.devId = param.devId!!
        doorCard.deptId = param.deptId!!

        doorCardMapper.updateById(doorCard)
        return Ref.ok()
    }

    /**查询本部门没有绑定卡的人员*/
    override fun listUnBindEmp(): CommonResult<List<DoorCardListUnBindEmpVo>>{

        val info = SystemUtil.getInfo()
        var list = doorCardMapper.listUnBindEmp(info.deptId)
        return Ref.ok(list)
    }

    /**门卡绑定人员*/
    override fun bindEmp(param: DoorCardBindEmpParam): CommonResult<Void>{
        var doorCard:DoorCard? = doorCardMapper.selectById(param.cardId)
        doorCard?:return Ref.error("错误的门卡id")
        if(doorCard.empId!=null){
            return Ref.error("该门卡已经绑定了人员")
        }
        doorCard.empId = param.empId
        doorCardMapper.updateById(doorCard)
        return Ref.ok()
    }

    /**门卡解绑员工*/
    override fun unBindEmp(param: DoorCardUnBindEmpParam): CommonResult<Void>{
        var doorCard:DoorCard? = doorCardMapper.selectById(param.cardId)
        doorCard?:return Ref.error("错误的门卡id")
        doorCard.empId = null
        doorCardMapper.updateById(doorCard)
        return Ref.ok()
    }

    /**门卡绑定门*/
    override fun bindDoor(param: DoorCardBindDoorParam): CommonResult<Void>{

        var doorCard:DoorCard? = doorCardMapper.selectById(param.cardId)
        doorCard?:return Ref.error("错误的门卡id")

        //如果要绑定的门是空数组.直接返回成功
        if(param.doorIdList.size==0){
            return Ref.ok()
        }

        //先删除已经存在doorId
        doorCardBindMapper.deleteByCardIdAndDoorIdList(param.cardId!!,param.doorIdList)
        //然后根据页面传过来的门id进行添加操作
        for(doorId in param.doorIdList){
            var bind = DoorCardBind()
            bind.doorId = doorId
            bind.cardId = param.cardId
            bind.createTime = Date()
            bind.expireTime = TimeUtil.parse1(param.expireTime)
            bind.timePickerStart = param.timePicker[0]
            bind.timePickerEnd = param.timePicker[1]
            bind.weekday = StringUtil.listInt2String(param.weekday)
            doorCardBindMapper.insert(bind)
        }
        return Ref.ok()
    }

    /**删除门的绑定*/
    override fun unBindDoor(param: DoorCardUnBindDoorParam): CommonResult<Void>{

        var bind:DoorCardBind? = doorCardBindMapper.selectById(param.bindId)
        bind?:return Ref.error("错误的绑定id")
        doorCardBindMapper.deleteById(param.bindId)
        return Ref.ok()
    }

    /**删除门卡*/
    override fun toDel(param: DoorCardToDelParam): CommonResult<Void>{
        var doorCard:DoorCard? = doorCardMapper.selectById(param.cardId)
        doorCard?:return Ref.error("错误的门卡id")

        if(doorCard.empId!=null){
            return Ref.error("该门卡已经绑定了员工,不能删除")
        }
        var bindList = doorCardBindMapper.listByCardId(param.cardId!!)
        if(bindList.size>0){
            return Ref.error("该门卡已经绑定了门,不能删除")
        }
        doorCardMapper.deleteById(param.cardId)
        return Ref.ok()
    }

    /**获取本部门所有的卡*/
    override fun listAll(): CommonResult<List<DoorCardListCardVo>>{

        val info = SystemUtil.getInfo()
        var cards = doorCardMapper.listAllByDept(info.deptId)
        var vos = cards.map {
            var vo = DoorCardListCardVo()
            vo.cardId = it.id
            vo.cardDevId = it.devId
            vo
        }
        return Ref.ok(vos)
    }

    /**批量绑定*/
    override fun batchBind(param: DoorCardBatchBindParam): CommonResult<Void>{

        println(JSON.toJSONString(param))

        if(param.cardIdList.size==0){
            return Ref.ok()
        }
        if(param.doorIdList.size==0){
            return Ref.ok()
        }

        for(cardId in param.cardIdList){
            //先删除已经存在doorId
            doorCardBindMapper.deleteByCardIdAndDoorIdList(cardId,param.doorIdList)
            //然后根据页面传过来的门id进行添加操作
            for(doorId in param.doorIdList){
                var bind = DoorCardBind()
                bind.doorId = doorId
                bind.cardId = cardId
                bind.createTime = Date()
                bind.expireTime = TimeUtil.parse1(param.expireTime)
                bind.timePickerStart = param.timePicker[0]
                bind.timePickerEnd = param.timePicker[1]
                bind.weekday = StringUtil.listInt2String(param.weekday)
                doorCardBindMapper.insert(bind)
            }
        }
        return Ref.ok()
    }

    /**绑定列表*/
    override fun listBindTable(param: DoorCardListBindTableParam)
            : CommonResult<DataGrid<DoorCardListBindTableVo>>{

        var bindList = doorCardBindMapper.listBind(param.start!!,param.num!!)
        var count = doorCardBindMapper.countListBind()
        var vos = bindList.map {
            var vo = DoorCardListBindTableVo()
            vo.bindId = it.id
            vo.createTime = TimeUtil.formater(it.createTime)
            vo.expireTime = TimeUtil.formater(it.expireTime)
            vo.timePickerEnd = it.timePickerEnd!!
            vo.timePickerStart = it.timePickerStart!!

            var weekday = it.weekday!!
            vo.weekday = weekday.replace("1","一")
                    .replace("2","二")
                    .replace("3","三")
                    .replace("4","四")
                    .replace("5","五")
                    .replace("6","六")
                    .replace("7","日")
            var door: Door? = doorMapper.selectById(it.doorId)
            vo.doorId = it.doorId
            vo.doorName = door?.name?:""
            vo.doorDevId = door?.devId?:""

            var card:DoorCard? = doorCardMapper.selectById(it.cardId)
            vo.cardId = it.cardId
            vo.cardDevId = card?.devId?:""
            vo.deptId = card?.deptId
            vo
        }
        var dataGrid = DataGrid<DoorCardListBindTableVo>()
        dataGrid.rows = vos
        dataGrid.total = count
        return Ref.ok(dataGrid)
    }

    /**批量删除*/
    override fun batchUnBind(param: DoorCardBatchUnBindParam): CommonResult<Void>{
        if(param.toDelId.size == 0){
            return Ref.error("还没有选择要删除的绑定")
        }
        doorCardBindMapper.deleteBatchIds(param.toDelId)
        return Ref.ok()
    }

    /**根据门id分页查询门卡列表*/
    override fun listByDoorId(param: DoorCardListByDoorIdParam)
            : CommonResult<DataGrid<DoorCardListByDoorIdVo>>{

        var list = cardTimeBindMapper.listByDoorId(param.doorId!!,
                param.start!!,param.num!!)

        var count = cardTimeBindMapper.countListByDoorId(param.doorId!!)

        var vos = list.map{
            var vo = DoorCardListByDoorIdVo()

            vo.cardId = it.cardId
            vo.bindTime = TimeUtil.formater3(it.createTime)
            var card = doorCardMapper.selectById(it.cardId)
            if(card!=null){
                vo.devId = card.devId
                vo.empId = card.empId
                var emp:SysEmp? = empMapper.selectById(card.empId)
                vo.empName = emp?.name?:""
                vo.createTime = TimeUtil.formater3(card.createTime)
                vo.deptId = card.deptId
                var dept:SysDept? = deptMapper.selectById(card.deptId)
                vo.deptName = dept?.shortName?:""
            }
            vo.timeId = it.timeId
            var time = openTimeMapper.selectById(it.timeId)
            if(time!=null){
                vo.timeName = time.name!!
                vo.endDate = TimeUtil.formater1(time.endDate)
                vo.startTime = time.startTime!!
                vo.endTime = time.endTime!!
                vo.weekday = time.weekday!!.replace("1","一")
                        .replace("2","二")
                        .replace("3","三")
                        .replace("4","四")
                        .replace("5","五")
                        .replace("6","六")
                        .replace("7","日")
                vo.opMode = time.opMode
                vo.opModeStr = when(time.opMode){
                    1 -> "单卡识别"
                    2 -> "卡加密码"
                    3 -> "定时开关门"
                    4 -> "双卡识别"
                    5 -> "定时报警"
                    6 -> "密码"
                    7 -> "首卡开门"
                    else -> ""
                }
            }
            vo
        }
        var dataGrid = DataGrid<DoorCardListByDoorIdVo>()
        dataGrid.total = count
        dataGrid.rows = vos
        return Ref.ok(dataGrid)
    }


    /**根据开放时间id查询绑定的门卡*/
    override fun listByTimeId(param: DoorCardListByTimeIdParam)
            : CommonResult<DataGrid<DoorCardListByTimeIdVo>>{

        var cards = doorCardMapper.listByTimeId(param.timeId!!,param.start!!,param.num!!)
        var count = doorCardMapper.countListByTimeId(param.timeId!!)
        var vos = cards.map {
            var vo = DoorCardListByTimeIdVo()
            vo.cardId = it.id
            vo.createTime = TimeUtil.formater3(it.createTime)
            vo.deptId = it.deptId
            vo.devId = it.devId
            vo.empId = it.empId
            var dept:SysDept? = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""
            var emp:SysEmp? = empMapper.selectById(it.empId)
            vo.empName = emp?.name?:""
            vo
        }
        var dataGrid = DataGrid<DoorCardListByTimeIdVo>()
        dataGrid.rows = vos
        dataGrid.total = count
        return Ref.ok(dataGrid)
    }
}